package com.ny.zmb.openapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ny.zmb.glxt.entity.PmStore;
import com.ny.zmb.glxt.service.IPmStoreService;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.entity.PmUserToken;
import com.ny.zmb.openapi.mapper.merchant.MerchantWithdrawMapper;
import com.ny.zmb.openapi.service.IPmMerchantService;
import com.ny.zmb.openapi.ysutil.YsHttpUtil;
import com.ny.zmb.openapi.ysutil.base.EYinShengBase;
import com.ny.zmb.openapi.ysutil.common.ServiceEnum;
import com.ny.zmb.openapi.ysutil.common.YsPayConstant;
import com.ny.zmb.openapi.ysutil.from.MerchantReportRequestCommand;
import com.ny.zmb.openapi.ysutil.from.merchant.*;
import com.ny.zmb.openapi.ysutil.publicInterface.QueryAmountReqCommand;
import com.ny.zmb.openapi.ysutil.utils.RequestNoUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 商户相关
 */
@RequestMapping("/ys/merchant")
@RestController
@CrossOrigin
public class YsMerchantController extends EYinShengBase {

    @Resource
    private YsHttpUtil ysHttpUtil;
    @Resource
    private IPmMerchantService pmMerchantService;
    @Resource
    private IPmStoreService pmStoreService;

    /**
     * 获取商户余额
     *
     * @return Result
     */
    @GetMapping("/queryBalances")
    public Result queryBalances() {
        PmUserToken pmUserToken = getUserToken();
        Integer userId = pmUserToken.getUserId();
        try {
            PmStore pmStore = pmStoreService.getInfoByUserId(userId);
            if (pmStore == null) {
                return Result.fail("获取用户信息失败");
            }
            JSONObject obj = new JSONObject();
            obj.put("merchantNo", pmStore.getBusinessNo());
            JSONObject result = pmMerchantService.queryBalances(obj);
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }

    /**
     * 绑定提现卡
     *
     * @param obj
     * @return
     */
    @PostMapping("/addBankAccount")
    public Result addBankAccount(@RequestBody MerchantAddBankAccount obj) {
        PmUserToken userDetailByToken = getUserToken();
        Integer userId = userDetailByToken.getUserId();
        obj.checkRequestNo();
        JSONObject result = pmMerchantService.addBankAccount(obj, userId);
        return Result.ok(result);
    }

    /**
     * 商户提现卡查询
     *
     * @param obj
     * @return
     */
    @PostMapping("/bankAccountQuery")
    public Result bankAccountQuery(@RequestBody MerchantBankAccountQuery obj) {
        PmUserToken userDetailByToken = getUserToken();
        Integer userId = userDetailByToken.getUserId();
        obj.checkRequestNo();
        JSONObject result = pmMerchantService.bankAccountQuery(obj, userId);
        return Result.ok(result);
    }

    /**
     * 商户结算信息变更
     *
     * @param obj
     * @return
     */
    @PostMapping("/changePayment")
    public Result changePayment(@RequestBody MerchantChangePayment obj) {
        PmUserToken userDetailByToken = getUserToken();
        Integer userId = userDetailByToken.getUserId();
        obj.checkRequestNo();
        JSONObject result = pmMerchantService.changePayment(obj, userId);
        return Result.ok(result);
    }

    /**
     * 商户提现申请
     *
     * @param obj
     * @return
     */
    @PostMapping("/merchantWithdraw")
    public Result merchantWithdraw(@RequestBody MerchantWithdraw obj) {
        PmUserToken userDetailByToken = getUserToken();
        Integer userId = userDetailByToken.getUserId();
        obj.checkRequestNo();
        JSONObject result = pmMerchantService.merchantWithdraw(obj, userId);
        return Result.ok(result);
    }

    @RequestMapping("/getList")
    public IPage<MerchantWithdraw> getList(MerchantWithdraw merchantWithdraw, @RequestParam(defaultValue = "1") int currentPage, @RequestParam(defaultValue = "10") int pageSize) {
        PmUserToken pmUserToken = getUserToken();
        merchantWithdraw.setUserId(pmUserToken.getUserId());
        IPage<MerchantWithdraw> page = new Page<>(currentPage, pageSize);
        QueryWrapper<MerchantWithdraw> queryWrapper = new QueryWrapper<>(merchantWithdraw);
        return pmMerchantService.page(page, queryWrapper);
    }

    /**
     * 商户提现查询
     *
     * @param requestNo 提现请求号
     * @return
     */
    @GetMapping("/queryWithdraw")
    public Result queryWithdraw(@RequestParam String requestNo) {
        PmUserToken userDetailByToken = getUserToken();
        Integer userId = userDetailByToken.getUserId();
        JSONObject result = pmMerchantService.queryWithdraw(requestNo, userId);
        return result == null ? Result.fail("未找到提现记录") : Result.ok(result);
    }

    /**
     * 商户报备
     *
     * @return
     */
    @PostMapping("report")
    public Result report(@RequestBody MerchantReportRequestCommand command) {
        try {
            if (null == command) {
                return Result.fail("参数有误");
            }
            JSONObject send = ysHttpUtil.send(ServiceEnum.MERCHANT_REPORT, YsPayConstant.ENV_PRD,
                    MERCHANT_PRX, MERCHANT_PRX_PWD, ServiceEnum.MERCHANT_REPORT.getApiName(), command);
            JSONObject result = send.getJSONObject("bizResponseJson");
            return Result.ok(result);
        } catch (Exception var) {
            return Result.fail(var.getMessage());
        }
    }
}
